﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace HYRElectric.Equipment
{
	internal class LayoutPlanManage
	{
		public LayoutPlanManage(Document doc)
		{
			this.m_doc = doc;
			this.m_dictViewCopySet = new Dictionary<ViewPlan, bool>();
			this.m_dictViewTemplate = new Dictionary<string, ViewPlan>();
			this.m_lstCatetoryTol = new List<Category>();
		}

		public void CopyViewPlan()
		{
			this.GetTolPlan();
			this.GetTolPlanTemplate();
			this.GetTolCatetories();
			if (this.m_dictViewCopySet.Count == 0)
			{
				return;
			}
			this.DictionarySort(ref this.m_dictViewCopySet);
			if (new DlgLayoutPlanCopySelect(ref this.m_dictViewCopySet).ShowDialog() != DialogResult.OK)
			{
				return;
			}
			this.CopyChangePlanName();
		}

		private void DictionarySort(ref Dictionary<ViewPlan, bool> dic)
		{
			if (dic.Count > 0)
			{
				List<KeyValuePair<ViewPlan, bool>> list = new List<KeyValuePair<ViewPlan, bool>>(dic);
				list.Sort(delegate(KeyValuePair<ViewPlan, bool> s1, KeyValuePair<ViewPlan, bool> s2)
				{
					string viewPlanParentName = DlgLayoutPlanCopySelect.GetViewPlanParentName(s1.Key);
					string viewPlanParentName2 = DlgLayoutPlanCopySelect.GetViewPlanParentName(s2.Key);
					return viewPlanParentName.CompareTo(viewPlanParentName2);
				});
				dic.Clear();
				foreach (KeyValuePair<ViewPlan, bool> keyValuePair in list)
				{
					dic.Add(keyValuePair.Key, keyValuePair.Value);
				}
			}
		}

		private void GetTolPlan()
		{
			foreach (ViewPlan viewPlan in (from ViewPlan vv in new FilteredElementCollector(this.m_doc).OfClass(typeof(ViewPlan))
			where !vv.IsTemplate
			select vv).ToList<ViewPlan>())
			{
				if (viewPlan.ViewName.IndexOf("出图") < 0)
				{
					this.m_dictViewCopySet.Add(viewPlan, true);
				}
			}
		}

		private void GetTolPlanTemplate()
		{
			string str = "";
			foreach (ViewPlan viewPlan in (from ViewPlan vv in new FilteredElementCollector(this.m_doc).OfClass(typeof(ViewPlan))
			where vv.IsTemplate
			select vv).ToList<ViewPlan>())
			{
				string text = viewPlan.ViewName;
				if (text.IndexOf("出图") >= 0 && text.IndexOf("HY-") >= 0)
				{
					str = str + text + "\n";
					text = text.Replace("HY-", "");
					text = text.Replace("平面出图", "");
					this.m_dictViewTemplate.Add(text, viewPlan);
				}
			}
		}

		private bool IsViewPlanExist(string strViewName)
		{
			return (from ViewPlan vv in new FilteredElementCollector(this.m_doc).OfClass(typeof(ViewPlan))
			where vv.ViewName == strViewName
			select vv).ToList<ViewPlan>().Count > 0;
		}

		private string GetLayoutPlanName(ViewPlan curModelPlan)
		{
			string viewName = curModelPlan.ViewName;
			string str;
			if (viewName.IndexOf("建模") >= 0)
			{
				str = viewName.Replace("建模", "出图");
			}
			else
			{
				str = viewName + "-出图";
			}
			string str2 = "";
			int num = 1;
			while (this.IsViewPlanExist(str + str2))
			{
				str2 = "-" + num.ToString();
				num++;
			}
			return str + str2;
		}

		private void CopyChangePlanName()
		{
			string str = "";
			foreach (KeyValuePair<ViewPlan, bool> keyValuePair in this.m_dictViewCopySet)
			{
				ViewPlan key = keyValuePair.Key;
				if (keyValuePair.Value)
				{
					Transaction transaction = new Transaction(this.m_doc, "CopyViewPlan");
					try
					{
						transaction.Start();
                        ElementId elementId = key.Duplicate((ViewDuplicateOption)2);
						ViewPlan viewPlan = this.m_doc.GetElement(elementId) as ViewPlan;
						string text = key.ViewName;
						str += text;
						str += "\n";
						text = this.GetLayoutPlanName(key);
						viewPlan.ViewName = text;
						Parameter parameter = viewPlan.GetParameter("视图分类-子");
						if (parameter != null)
						{
							parameter.Set("02出图");
						}
						else
						{
							parameter = viewPlan.GetParameter("子规程");
							if (parameter != null)
							{
								parameter.Set("出图");
							}
						}
						this.SetViewPlanFromTemplate(viewPlan);
						transaction.Commit();
					}
					catch
					{
						transaction.RollBack();
					}
				}
			}
		}

		private void GetTolCatetories()
		{
			Categories categories = this.m_doc.Settings.Categories;
			categories.GetEnumerator();
			foreach (object obj in categories)
			{
				Category item = (Category)obj;
				this.m_lstCatetoryTol.Add(item);
			}
		}

		private void SetViewPlanFromTemplate(ViewPlan viewPlanCopy)
		{
			if (this.m_dictViewTemplate.Count == 0)
			{
				return;
			}
			string viewName = viewPlanCopy.ViewName;
			ViewPlan viewPlan = null;
			foreach (KeyValuePair<string, ViewPlan> keyValuePair in this.m_dictViewTemplate)
			{
				string key = keyValuePair.Key;
				if (viewName.IndexOf(key) >= 0)
				{
					viewPlan = keyValuePair.Value;
					break;
				}
			}
			if (viewPlan == null)
			{
				return;
			}
			if (viewPlanCopy.CanModifyDetailLevel())
			{
				viewPlanCopy.DetailLevel = viewPlan.DetailLevel;
			}
			if (viewPlanCopy.CanModifyDisplayStyle())
			{
				viewPlanCopy.DisplayStyle = viewPlan.DisplayStyle;
			}
			foreach (Category category in this.m_lstCatetoryTol)
			{
				if (viewPlan.IsCategoryOverridable(category.Id))
				{
					try
					{
						viewPlanCopy.SetYJKVisibility(category, viewPlan.GetYJKVisibility(category));
					}
					catch (Exception)
					{
					}
				}
			}
		}

		private Document m_doc;

		private Dictionary<ViewPlan, bool> m_dictViewCopySet;

		private Dictionary<string, ViewPlan> m_dictViewTemplate;

		private List<Category> m_lstCatetoryTol;
	}
}
